package com.ysd.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.exception.RRException;
import com.ysd.modules.app.dao.CoursesDao;
import com.ysd.modules.app.dao.PracticeResultDao;
import com.ysd.modules.app.dao.RankingDao;
import com.ysd.modules.app.dao.StudentDao;
import com.ysd.modules.app.entity.ClassesEntity;
import com.ysd.modules.app.entity.CourseEntity;
import com.ysd.modules.app.entity.PracticeResult;
import com.ysd.modules.app.entity.Ranking;
import com.ysd.modules.app.entity.StudentEntity;
import com.ysd.modules.app.service.ClassesService;
import com.ysd.modules.app.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Autowired
    private ClassesService classesService;
    @Resource
    private CoursesDao coursesDao;
    @Autowired
    private PracticeResultDao practiceResultDao;
    
    @Autowired
    private RankingDao rankingDao;

    @Override
    public StudentEntity getStudentById(Integer stuId) {
        StudentEntity student = baseMapper.selectById(stuId);
        ClassesEntity classes = classesService.getByStudentId(stuId);
        List<Ranking> rank = rankingDao.getRank();
        for(Ranking r:rank) {
        	if(r.getStuId() == stuId) {
        		student.setRanking(r.getStuRank());
        	}
        }
        student.setClassId(classes);
        return student;
    }

    /**
     * 修改当前正在进行的课程(由于是选择,所以不添加状态验证)
     *
     * @param stuId
     * @param cid
     * @return
     */
    @Override
    public StudentEntity updateStudent(Integer stuId, Integer cid) {
        StudentEntity studentEntity = baseMapper.selectById(stuId);
        if (studentEntity == null) {
            throw new RRException("没有该学生");
        }
        CourseEntity courseEntity = coursesDao.selectById(cid);
        if (courseEntity == null) {
            throw new RRException("没有该课程");
        }
        studentEntity.setCourseId(cid);
        studentEntity.setCourseName(courseEntity.getName());
        baseMapper.updateById(studentEntity);
        return baseMapper.selectById(stuId);
    }


}
 